package com.cskaoyan.mall.pay.service.remote;


import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.pay.constants.PayResult;
import com.cskaoyan.mall.pay.dto.PayOrderCancelRequest;
import com.cskaoyan.mall.pay.dto.PayOrderCancelResponse;
import com.cskaoyan.mall.pay.service.PayOrderCancelService;
import com.cskaoyan.mall.pay.service.impl.PayHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PayOrderServiceImpl implements PayOrderCancelService {

    @Autowired
    PayHelper payHelper;
    @PutMapping("/rpc/pay/cancel")
    public PayOrderCancelResponse payOrderCancel(@RequestBody PayOrderCancelRequest request) {
        PayOrderCancelResponse response = new PayOrderCancelResponse();
        try {
            request.requestCheck();
            String orderId = request.getOrderId();
            boolean result = payHelper.test_trade_cancel(orderId);
            if (result){
                response.setCode(PayResult.FAIL.getCode());
                response.setMsg(PayResult.FAIL.getDesc());
                return response;
            }
            response.setCode(SysRetCodeConstants.SYSTEM_ERROR.getCode());
            response.setMsg(SysRetCodeConstants.SYSTEM_ERROR.getMessage());
        } catch (Exception e) {
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
